<template>
  <div class="app-container">
    <!-- 搜索工作栏 -->
    <el-form
      v-show="showSearch"
      ref="queryForm"
      :model="queryParams"
      size="small"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="企业名称" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入企业名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属行业" prop="industry">
        <el-input
          v-model="queryParams.industry"
          placeholder="请输入所属行业"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="城市" prop="city">
        <el-input
          v-model="queryParams.city"
          placeholder="请输入城市"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="职位名称" prop="jobName">
        <el-input
          v-model="queryParams.jobName"
          placeholder="请输入职位名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学历要求" prop="education">
        <el-input
          v-model="queryParams.education"
          placeholder="请输入学历要求"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="职位状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择职位状态"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in this.getDictDatas(DICT_TYPE.MEMBER_JOB_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="置顶状态" prop="topStatus">
        <el-input
          v-model="queryParams.topStatus"
          placeholder="请输入置顶状态"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发布时间">
        <el-date-picker
          v-model="dateRangePublishTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          @click="handleQuery"
        >搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['member:job:create']"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['member:job:export']"
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar
        :show-search.sync="showSearch"
        @queryTable="getList"
      />
    </el-row>

    <!-- 列表 -->
    <el-table v-loading="loading" :data="list">
      <el-table-column label="企业名称" align="center" prop="companyName" />
      <el-table-column label="所属行业" align="center" prop="industry" />
      <el-table-column label="城市" align="center" prop="city" />
      <el-table-column label="职位名称" align="center" prop="jobName" />
      <el-table-column label="薪资类型" align="center" prop="salaryUnit">
        <template slot-scope="scope">
          <dict-tag
            :type="DICT_TYPE.SALARY_TYPE"
            :value="scope.row.salaryUnit"
          />
        </template>
      </el-table-column>
      <el-table-column label="学历要求" align="center" prop="education" />
      <el-table-column label="工作地址" align="center" prop="workAddress" />
      <el-table-column label="职位状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag
            :type="DICT_TYPE.MEMBER_JOB_STATUS"
            :value="scope.row.status"
          />
        </template>
      </el-table-column>
      <el-table-column label="职位点击量" align="center" prop="clickCount" />
      <el-table-column label="职位投递量" align="center" prop="postCount" />
      <el-table-column label="置顶状态" align="center" prop="topStatus" />
      <el-table-column
        label="更新时间"
        align="center"
        prop="updateTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="发布时间"
        align="center"
        prop="publishTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ moment(scope.row.publishTime).format('yyyy-MM-DD') }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['member:job:update']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            v-hasPermi="['member:job:delete']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="企业id" prop="companyId">
          <el-input v-model="form.companyId" placeholder="请输入企业id" />
        </el-form-item>
        <el-form-item label="企业名称" prop="companyName">
          <el-input v-model="form.companyName" placeholder="请输入企业名称" />
        </el-form-item>
        <el-form-item label="企业logo">
          <imageUpload v-model="form.companyLogo" />
        </el-form-item>
        <el-form-item label="所属行业" prop="industry">
          <el-input v-model="form.industry" placeholder="请输入所属行业" />
        </el-form-item>
        <el-form-item label="城市" prop="city">
          <el-input v-model="form.city" placeholder="请输入城市" />
        </el-form-item>
        <el-form-item label="职位名称" prop="jobName">
          <el-input v-model="form.jobName" placeholder="请输入职位名称" />
        </el-form-item>
        <el-form-item label="薪资类型" prop="salaryUnit">
          <el-select v-model="form.salaryUnit" placeholder="请选择薪资类型">
            <el-option
              v-for="dict in this.getDictDatas(DICT_TYPE.SALARY_TYPE)"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="最低薪资" prop="lowestSalary">
          <el-input v-model="form.lowestSalary" placeholder="请输入最低薪资" />
        </el-form-item>
        <el-form-item label="最高薪资" prop="highestSalary">
          <el-input v-model="form.highestSalary" placeholder="请输入最高薪资" />
        </el-form-item>
        <el-form-item label="部门" prop="department">
          <el-input v-model="form.department" placeholder="请输入部门" />
        </el-form-item>
        <el-form-item label="学历要求" prop="education">
          <el-input v-model="form.education" placeholder="请输入学历要求" />
        </el-form-item>
        <el-form-item label="hr手机" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入hr手机" />
        </el-form-item>
        <el-form-item label="工作周期" prop="workPeriod">
          <el-input v-model="form.workPeriod" placeholder="请输入工作周期" />
        </el-form-item>
        <el-form-item label="hr邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入hr邮箱" />
        </el-form-item>
        <el-form-item label="招聘对象" prop="identify">
          <el-select v-model="form.identify" placeholder="请选择招聘对象">
            <el-option
              v-for="dict in this.getDictDatas(DICT_TYPE.RECRUITMENT_TARGET)"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="实习周期" prop="practicePeriod">
          <el-input
            v-model="form.practicePeriod"
            placeholder="请输入实习周期"
          />
        </el-form-item>
        <el-form-item label="招聘人数" prop="recruitmentCount">
          <el-input
            v-model="form.recruitmentCount"
            placeholder="请输入招聘人数"
          />
        </el-form-item>
        <el-form-item label="工作地址" prop="workAddress">
          <el-input v-model="form.workAddress" placeholder="请输入工作地址" />
        </el-form-item>
        <el-form-item label="工作坐标" prop="workCoordinate">
          <el-input
            v-model="form.workCoordinate"
            placeholder="请输入工作坐标"
          />
        </el-form-item>
        <el-form-item label="福利标签" prop="welfareTags">
          <el-input v-model="form.welfareTags" placeholder="请输入福利标签" />
        </el-form-item>
        <el-form-item label="职位描述">
          <editor v-model="form.jobDescription" :min-height="192" />
        </el-form-item>
        <el-form-item label="职位状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择职位状态">
            <el-option
              v-for="dict in this.getDictDatas(DICT_TYPE.MEMBER_JOB_STATUS)"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="职位点击量" prop="clickCount">
          <el-input v-model="form.clickCount" placeholder="请输入职位点击量" />
        </el-form-item>
        <el-form-item label="职位投递量" prop="postCount">
          <el-input v-model="form.postCount" placeholder="请输入职位投递量" />
        </el-form-item>
        <el-form-item label="审核描述" prop="checkMessage">
          <el-input v-model="form.checkMessage" placeholder="请输入审核描述" />
        </el-form-item>
        <el-form-item label="置顶状态" prop="topStatus">
          <el-input v-model="form.topStatus" placeholder="请输入置顶状态" />
        </el-form-item>
        <el-form-item label="审核时间" prop="checkTime">
          <el-date-picker
            v-model="form.checkTime"
            clearable
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择审核时间"
          />
        </el-form-item>
        <el-form-item label="发布时间" prop="publishTime">
          <el-date-picker
            v-model="form.publishTime"
            clearable
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择发布时间"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  createJob,
  updateJob,
  deleteJob,
  getJob,
  getJobPage,
  exportJobExcel
} from '@/api/member/job'
import ImageUpload from '@/components/ImageUpload'
import Editor from '@/components/Editor'
import { DICT_TYPE, getDictDatas } from '@/utils/dict'
import moment from 'moment'
export default {
  name: 'Job',
  components: {
    ImageUpload,
    Editor
  },
  data() {
    return {
      moment: moment,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 招聘职位列表
      list: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      dateRangePublishTime: [],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        companyName: null,
        industry: null,
        city: null,
        jobName: null,
        education: null,
        status: null,
        topStatus: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        companyId: [
          {
            required: true,
            message: '企业id不能为空',
            type: 'number',
            trigger: 'blur'
          }
        ],
        companyName: [
          { required: true, message: '企业名称不能为空', trigger: 'blur' }
        ],
        companyLogo: [
          { required: true, message: '企业logo不能为空', trigger: 'blur' }
        ],
        industry: [
          { required: true, message: '所属行业不能为空', trigger: 'blur' }
        ],
        city: [{ required: true, message: '城市不能为空', trigger: 'blur' }],
        jobName: [
          { required: true, message: '职位名称不能为空', trigger: 'blur' }
        ],
        salaryUnit: [
          { required: true, message: '薪资类型不能为空', trigger: 'change' }
        ],
        lowestSalary: [
          {
            required: true,
            message: '最低薪资不能为空',
            type: 'number',
            trigger: 'blur'
          }
        ],
        highestSalary: [
          {
            required: true,
            message: '最高薪资不能为空',
            type: 'number',
            trigger: 'blur'
          }
        ],
        education: [
          { required: true, message: '学历要求不能为空', trigger: 'blur' }
        ],
        workPeriod: [
          { required: true, message: '工作周期不能为空', trigger: 'blur' }
        ],
        identify: [
          { required: true, message: '招聘对象不能为空', trigger: 'change' }
        ],
        practicePeriod: [
          { required: true, message: '实习周期不能为空', trigger: 'blur' }
        ],
        recruitmentCount: [
          {
            required: true,
            message: '招聘人数不能为空',
            type: 'number',
            trigger: 'blur'
          }
        ],
        workAddress: [
          { required: true, message: '工作地址不能为空', trigger: 'blur' }
        ],
        workCoordinate: [
          { required: true, message: '工作坐标不能为空', trigger: 'blur' }
        ],
        welfareTags: [
          { required: true, message: '福利标签不能为空', trigger: 'blur' }
        ],
        jobDescription: [
          { required: true, message: '职位描述不能为空', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '职位状态不能为空', trigger: 'change' }
        ],
        clickCount: [
          {
            required: true,
            message: '职位点击量不能为空',
            type: 'number',
            trigger: 'blur'
          }
        ],
        postCount: [
          {
            required: true,
            message: '职位投递量不能为空',
            type: 'number',
            trigger: 'blur'
          }
        ],
        checkMessage: [
          { required: true, message: '审核描述不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true
      // 处理查询参数
      const params = { ...this.queryParams }
      this.addBeginAndEndTime(params, this.dateRangePublishTime, 'publishTime')
      // 执行查询
      getJobPage(params).then((response) => {
        this.list = response.data.list
        this.total = response.data.total
        this.loading = false
      })
    },
    /** 取消按钮 */
    cancel() {
      this.open = false
      this.reset()
    },
    /** 表单重置 */
    reset() {
      this.form = {
        companyId: undefined,
        companyName: undefined,
        companyLogo: undefined,
        industry: undefined,
        city: undefined,
        jobName: undefined,
        salaryUnit: undefined,
        lowestSalary: undefined,
        highestSalary: undefined,
        department: undefined,
        education: undefined,
        phone: undefined,
        workPeriod: undefined,
        email: undefined,
        identify: undefined,
        practicePeriod: undefined,
        recruitmentCount: undefined,
        workAddress: undefined,
        workCoordinate: undefined,
        welfareTags: undefined,
        jobDescription: undefined,
        status: undefined,
        clickCount: undefined,
        postCount: undefined,
        checkMessage: undefined,
        topStatus: undefined,
        checkTime: undefined,
        publishTime: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRangePublishTime = []
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加招聘职位'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id
      getJob(id).then((response) => {
        this.form = response.data
        this.open = true
        this.title = '修改招聘职位'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (!valid) {
          return
        }
        // 修改的提交
        if (this.form.id != null) {
          updateJob(this.form).then((response) => {
            this.$modal.msgSuccess('修改成功')
            this.open = false
            this.getList()
          })
          return
        }
        // 添加的提交
        createJob(this.form).then((response) => {
          this.$modal.msgSuccess('新增成功')
          this.open = false
          this.getList()
        })
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id
      this.$modal
        .confirm('是否确认删除招聘职位编号为"' + id + '"的数据项?')
        .then(function() {
          return deleteJob(id)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      const params = { ...this.queryParams }
      params.pageNo = undefined
      params.pageSize = undefined
      this.addBeginAndEndTime(params, this.dateRangePublishTime, 'publishTime')
      // 执行导出
      this.$modal
        .confirm('是否确认导出所有招聘职位数据项?')
        .then(() => {
          this.exportLoading = true
          return exportJobExcel(params)
        })
        .then((response) => {
          this.$download.excel(response, '招聘职位.xls')
          this.exportLoading = false
        })
        .catch(() => {})
    }
  }
}
</script>
